<?php

include_once "model.php";


$depth=0;
if (isset($_GET["depth"])){
    $depth=$_GET["depth"];
}


function render($node) {
    global $depth;
    if ($node[NODE_DEPTH] == $depth){
	echo '<p>'.$node[NODE_CONTENT].'</p>';
	}
}

function helper($model, $left_id, $right_id) {
	$middle = $model->get_middle($left_id, $right_id);
	if ($middle != NULL){
		helper($model, $left_id, $middle[NODE_ID]);
		render($middle);
		helper($model, $middle[NODE_ID], $right_id);
	}
}


$start = $model->get_node(1);
$end = $model->get_node(2);
?>
<?
include("inc/header.inc");
?>
<?
render($start);
helper($model, $start[NODE_ID], $end[NODE_ID]);
render($end);

$depthinfo = "<b>Current story depth: %d</b>";
printf($depthinfo,$depth);
echo "<br>";
    
    
if ($depth!=0){
    $prev = "<a href=\"depth-viewer.php?depth=%d\">It's time to wake up!</a>";
    printf($prev,$depth-1);
}
echo "<br>";

$next = "<a href=\"depth-viewer.php?depth=%d\">We need to go deeper!</a>";
printf($next,$depth+1);


?>

<?
include("inc/footer.inc");
?>

<script type="text/javascript" src="js/viewer.js"></script>
